/*
 * public class EditBillboardBean
 * author: adam barney
 * backing bean that takes care of all billboard edit operations.
 */

/*
 * This file is part of WhatItDo.
 *
 * WhatItDo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WhatItDo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WhatItDo.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.sourceforge.whatitdo.billboardWeb;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;

import net.sourceforge.whatitdo.billboard.model.Billboard;
import net.sourceforge.whatitdo.billboard.model.DataHelper;

public class EditBillboardBean {

    private String id = null;
    private String name = null;
    private String tickerText = new String();
    private ArrayList<String> slideshowText = new ArrayList<String>();
    // private SelectItem[] slideshowText;
    private String selectedSSText = new String();
    private Billboard billboard = new Billboard();
    private Collection<SelectItem> allSSTexts;
    private boolean isPublished = false;
    /*
     * oldSelectedSSText is set in editSlideAction, and used in saveSSTextAction
     */
    private String oldSelectedSSText = new String();

    public String getId() {
	return id;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getTickerText() {
	// return billboard.getTickerText();
	return this.tickerText;
    }

    public void setTickerText(String tickerText) {
	if (!(tickerText == null || tickerText.equals(""))) {
	    tickerText.trim();
	}
	this.tickerText = tickerText;
    }

    public ArrayList<String> getSlideshowText() {
	return this.slideshowText;
    }

    public void setSlideshowText(ArrayList<String> slideshowText) {
	this.slideshowText = slideshowText;
    }

    public String getSelectedSSText() {
	return this.selectedSSText;
    }

    public void setSelectedSSText(String selectedSSText) {
	if (!(selectedSSText == null || selectedSSText.equals(""))) {
	    selectedSSText.trim();
	}
	this.selectedSSText = selectedSSText;
    }

    public boolean getIsPublished() {
	return isPublished;
    }

    public void setIsPublished(boolean newValue) {
	this.isPublished = newValue;
	if (newValue) {
	    System.out.println("Form has set isPublished = true");
	} else {
	    System.out.println("Form has set isPublished = false");
	}

    }

    // private void loadBillboard(String id) {
    // this.billboard.loadBillboard(id);
    // this.tickerText = billboard.getTickerText();
    // this.slideshowText = billboard.getSlideshowText();
    // /*
    // * TODO: set the image
    // */
    //
    // }

    public void setLoadAction(String name) {
	System.out.println("Made it to setLoadAction. Name is: " + name);
	setName(name);
	System.out.println("Name is now: " + name);
	setId(DataHelper.getBillboardId(name));
	System.out.println("ID is: " + id);
	this.billboard.loadBillboard(this.id);
	setSlideshowText(billboard.getSlideshowText());
	setTickerText(billboard.getTickerText());

	System.out.println("This ID = " + this.id);
	System.out.println("ID = " + id);

    }

    public String getLoadAction() {
	return this.name;
    }

    /*
     * editSlideAction, deleteSlideAction, and addSlideAction are all called
     * from editBillboard.jsp.
     */
    public String editSlideAction() {
	System.out.println("editSlideAction has been called on slide: "
		+ selectedSSText);
	/*
	 * Must initialize oldSelectedSlide for the purposes of saving changes.
	 */
	oldSelectedSSText = selectedSSText;
	return "editSlide";
    }

    public String deleteSlideAction() {
	System.out.println("deleteSlideAction has been called on slide: "
		+ selectedSSText);
	for (int i = 0; i < slideshowText.size(); i++) {
	    if (selectedSSText.equals(slideshowText.get(i))) {
		System.out.println("deleteSlideAction is removing : "
			+ slideshowText.get(i)
			+ " from ArrayList<String> slideshowText.");
		slideshowText.remove(i);
	    }
	}
	return "deleteSlide";
    }

    public String addSlideAction() {
	oldSelectedSSText = null;
	selectedSSText = null;
	return "editSlide";
    }

    public String saveSSTextAction() {
	if (selectedSSText == "") {
	    return "cancel";
	}
	/*
	 * This is kind of a hack. I'm not proud of it. The if(!updated) block
	 * will perform an ArrayList.add() if oldSelectedSSText does not match
	 * an existing element in the ArrayList<String> slideshowText.
	 */
	boolean updated = false;
	for (int i = 0; i < slideshowText.size(); i++) {
	    if (slideshowText.get(i).equals(oldSelectedSSText)) {
		slideshowText.set(i, selectedSSText);
		updated = true;
	    }
	}
	if (!updated) {
	    // Creating new slide
	    slideshowText.add(selectedSSText.trim());
	}
	return "save";
    }

    public String saveAction() {
	System.out.println("saveAction has been called");
	billboard.setTickerText(this.tickerText);
	billboard.setName(this.name);
	billboard.setPublished(isPublished);
	billboard.saveBillboard();
	return "save";
    }

    public Collection<SelectItem> getSlideshowCollection() {
	allSSTexts = new ArrayList<SelectItem>();
	if (slideshowText.size() != 0) {
	    for (int i = 0; i < slideshowText.size(); i++) {
		allSSTexts.add(new SelectItem(slideshowText.get(i)));
	    }
	    return allSSTexts;
	} else {
	    System.out.println("There are no SlideshowTexts");
	    return allSSTexts;
	}
    }
    
    public void deleteBillboardAction() {
	System.out.println("Attempting to delete: " + name);
	System.out.println("It's ID is: " + id);
	DataHelper.deleteBillboard(id);
    }

}
